from flask import Blueprint, request

from controllers import R
from mauth import HasPerm
from services.auth_service import AuthService
from validators.login_validator import LoginForm

# 声明一个蓝图
auth = Blueprint("auth", __name__, url_prefix="/sys")
# 声明一个角色业务服务
auth_service = AuthService()


@auth.route("/login", methods=["POST"])
def auth_login():
    """
    登录
    :return:
    """
    form = LoginForm()
    form.validate_for_api()
    return R.data(auth_service.login(form))


@auth.route("/logout", methods=["POST"])
def auth_logout():
    """
    退出
    :return:
    """
    return R.data(auth_service.logout())


@auth.route("/playUser", methods=["POST"])
@HasPerm(access="sys:playUser", name="扮演用户")
def play_user():
    """
    扮演用户
    :return:
    """
    userId = request.json.get("userId")
    return R.data(auth_service.play_user(userId))


@auth.route("/unPlayUser", methods=["POST"])
@HasPerm(name="退出扮演用户")
def un_play_user():
    """
    退出扮演用户
    :return:
    """
    return R.data(auth_service.un_play_user())


@auth.route("/getCaptchaOpenFlag", methods=["POST"])
def auth_get_captcha_open_flag():
    """
    获取登录图片验证码是否启用标识
    :return:
    """
    return R.data({"flag": False})
